﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using HNQXYJ.DAL;
using System.Security.Cryptography;
using HNQXYJ.Model;

namespace HNQXYJ.BLL
{
    public class UsersBLL
    {
        /// <summary>
        /// 获取终端数据表中的数据
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static DataTable GetUsersTable(string procName, params SqlParameter[] paras)
        {
            DataTable dt;
            if (procName != null)
            {
                dt = UsersDAL.GetDataTable(procName, paras);
            }
            else
            {
                dt = null;
            }
            return dt;
        }

        /// <summary>
        /// 通过存储过程执行非查询语句
        /// </summary>
        /// <param name="procName">存储过程</param>
        /// <param name="paras">参数</param>
        /// <returns>返回受影响的行数</returns>
        public static int ExecNoQuery(string procName, params SqlParameter[] paras)
        {
            int res = UsersDAL.ExecNonQuery(procName, paras);
            return res;
        }

        /// <summary>
        /// 用户密码加密
        /// </summary>
        /// <param name="sDataIn"></param>
        /// <returns></returns>
        public static string GetMD5(string sDataIn)
        {
            MD5CryptoServiceProvider md5 =
                new MD5CryptoServiceProvider();
            byte[] bytValue, bytHash;
            bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
            bytHash = md5.ComputeHash(bytValue);
            md5.Clear();
            string sTemp = "";
            for (int i = 0; i < bytHash.Length; i++)
            {
                sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
            }
            return sTemp.ToLower();
        }



        /// <summary>
        /// 添加新用户，先判断用户是否存在 添加成功则返回一个大于0的数
        /// </summary>
        /// <param name="user"></param>
        /// <returns>res 大于0 则说明添加成功 ，否则说明添加失败 </returns>
        public int AddNew(Users user)
        {
            UsersDAL ud=new UsersDAL();
            Users us = ud.GetUserByUserName(user.UserName);
            int res = 0;
            if (us == null)//用户名不存在则可以添加
            {
               res= ud.AddNewUser(user);
               
            }
            return res;
        }

        /// <summary>
        /// 提取用户列表
        /// </summary>
        /// <returns></returns>
        public List<Users> GetUsersListAll()
        {
            List<Users> list = new List<Users>();
            UsersDAL ud=new UsersDAL();
            list = ud.GetUsersListAll();
            return list;
        }

        /// <summary>
        /// 根据用户id删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteUserById(int id)
        {
            bool flage = false;
            UsersDAL ud=new UsersDAL();
            flage = ud.DeleteUserById(id);
            return flage;
 
        }

        /// <summary>
        /// 根据用户的id 得到一个用户对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Users GetUserDataById(int id)
        {
            Users user = new Users();
            UsersDAL ud = new UsersDAL();
            user = ud.GetUserDataById(id);
            return user;
        }

        /// <summary>
        /// 要根据用户的id来更新用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool UpdateUserById(Users user)
        {
            bool flage = false;
            UsersDAL ud = new UsersDAL();
            flage = ud.UPdateUserById(user);
            return flage;

 
        }

        /// <summary>
        /// 修改用户自己的的密码 要求必须先输入正确旧密码
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool UpateUserPassword(Users user)
        {
            bool flage = false;
            flage = new UsersDAL().UpdateUserPassword(user);
            return flage;
 
        }

        /// <summary>
        /// 判断用户是否存在 存在（用户名和密码都正确）返回true 否则返回false
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public bool ValidateUser(string username, string password)
        {
            UsersDAL ud = new UsersDAL();
            Users user = ud.GetUserByUserName(username);
            if (user == null)
            {
                return false;
            }
            if (GetMD5(password) != user.Password)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
             
        /// <summary>
        /// 根据用户名获得用户数据
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public Users GetUserByUserName(string username)
        {
            Users user = new UsersDAL().GetUserByUserName(username);
            return user;
        }

        /// <summary>
        /// 根据用户权限获取对应的用户列表
        /// </summary>
        /// <param name="ranklevel"></param>
        /// <returns></returns>
        public List<Users> GetUserListByRankLevel(string ranklevel)
        {
            List<Users> list = new UsersDAL().GetUserListByRankLevel(ranklevel);
            return list;
        }
    }
}